package com.example.last.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.last.common.Constants;
import com.example.last.common.enums.ErrorCode;
import com.example.last.dao.AdminDao;
import com.example.last.entity.Account;
import com.example.last.entity.Admin;
import com.example.last.entity.Params;
import com.example.last.exception.CustomException;
import com.example.last.utils.JwtTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminService {
    @Resource
    private AdminDao adminDao;

    public Admin login(Account account){
        //查询获取用户
        Admin dbUser = adminDao.findByNameAndPassword(account.getUsername(),account.getPassword());

        if(ObjectUtil.isNull(dbUser))  {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!dbUser.getPassword().equals(account.getPassword())){
            throw new CustomException(ErrorCode.USER_ACCOUNT_ERROR);
        }
        //生成jwtToken令牌
        String token = JwtTokenUtils.genToken(dbUser.getId() + "-" + dbUser.getRole(),dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;

    }

    //注册
//    public void addRegister(Admin1 admin1){
//        Admin1 user = admin1Dao.findByName(admin1.getUsername());
//        if(ObjectUtil.isNotNull(user)) {
//            throw new CustomException(ErrorCode.USER_EXIST_ERROR);
//        }
//        if(ObjectUtil.isEmpty(user.getPassword())) {
//            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
//        }
//        if(ObjectUtil.isEmpty(user.getUsername())) {
//            user.setUsername(user.getUsername());
//        }
//        user.setRole(RoleEnum.ADMIN.name());
//        admin1Dao.addRegister(user);
//    }


    public PageInfo<Admin> findBySearch(@Param("params") Params params){
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Admin> list =  adminDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Admin admin){
        //用户判空
        if(admin.getUsername() == null || "" .equals(admin.getUsername())){
            throw new CustomException(ErrorCode.USER_NOT_ERROR);
        }
        //用户判重
        Admin user = adminDao.findByName(admin.getUsername());
        if(user != null){
            throw new CustomException(ErrorCode.USER_EXIST_ERROR);
        }
        if(admin.getPassword() == null){
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD) ;
        }
        adminDao.add(admin);
    }

    public void updatePassword(Account account) {
        Admin dbAdmin = adminDao.selectByUsername(account.getUsername());
        if(ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!account.getPassword().equals(dbAdmin.getPassword())) {
            throw new CustomException(ErrorCode.PARAM_PASSWORD_ERROR);
        }
        adminDao.updatePassword(account);
    }

    public void update(Admin admin){
        adminDao.update(admin);
    }

    public void delete(Integer id){
        adminDao.delete(id);
    }

    public Admin findById(Integer id){
        return adminDao.selectByPrimaryKey(id);
    }

    public Admin findByName(String username) {
        return adminDao.findByName(username);
    }
}
